package com.generator.aboutUser.user.controller;

import com.generator.aboutUser.user.pojo.Users;
import com.generator.aboutUser.user.service.impl.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @version 1.0
 * @auther wumei
 * @date 2022/10/01
 * 星期六 14:48
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    public UserService getUserService() {
        return userService;
    }
    public void setUserService(UserService userService) {
        this.userService = userService;
    }


    /**
     * 登录验证，添加session
     * @param user
     * @return
     */
    @ResponseBody
    @RequestMapping("/login")
    public Users login(Users user, HttpSession session){
        if (user != null){
            Users loginUser = userService.login(user);
            session.setAttribute("user",loginUser);
            return loginUser;
        }
        return null;
    }

    /**
     * 获取当前登录用户信息
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping("/getLoginUser")
    public Users getLoginUser(HttpSession session){
        Users loginUser = (Users) session.getAttribute("user");
        return loginUser;
    }


    /**
     * 退出登录，删除session
     * @param session
     */
    @RequestMapping("/loginOut")
    public String loginOut(HttpSession session){
        session.invalidate();
        return "redirect:/login.html";
    }


}
